//
//  EnableSavingThrow.m
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 13/05/2007.
//  Copyright 2007 Matthew Hutton
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "EnableSavingThrow.h"


@implementation EnableSavingThrow
+ (Class)transformedValueClass{
	return [NSNumber class];
}
+ (BOOL)allowsReverseTransformation;{
	return NO;
}
- (id)transformedValue:(id)value{
	NSManagedObject *cs=[value valueForKey:@"currentSpell"];
	NSManagedObject *spell=[[cs valueForKey:@"spellClass"] valueForKey:@"spell"];
	//first check spell type isn't bigger than 4 or a healing spell.
	if(![[spell valueForKey:@"subtype"] isEqualToString:@"Damage"]){
		//NSLog(@"returning no");
		return [NSNumber numberWithBool:NO];
	}
	if([[spell valueForKey:@"spellResistance"] boolValue] && [[[value valueForKey:@"character"] valueForKey:@"spellResistance"] boolValue]){
		//suffers SR.
		if([[spell valueForKey:@"attackRoll"] boolValue]){
			//suffers an Attack Roll.
			if([[value valueForKey:@"passesSpellResistance"] boolValue] && [[value valueForKey:@"passesAttackRoll"] boolValue] && [[spell valueForKey:@"saveEffect"] intValue]>0){
				//both selected and ticked.
				return [NSNumber numberWithBool:YES];
			}else{
				return [NSNumber numberWithBool:NO];
			}
		}else if([[value valueForKey:@"passesSpellResistance"] boolValue] && [[spell valueForKey:@"saveEffect"] intValue]>0){
			//no attack roll, but SR.
			return [NSNumber numberWithBool:YES];
		}else{
			return [NSNumber numberWithBool:NO];
		}
	}else{
		//no SR
		if([[spell valueForKey:@"attackRoll"] boolValue]){
			//suffers an Attack Roll.
			if([[value valueForKey:@"passesAttackRoll"] boolValue] && [[spell valueForKey:@"saveEffect"] intValue]>0){
				//both selected and ticked.
				return [NSNumber numberWithBool:YES];
			}else{
				return [NSNumber numberWithBool:NO];
			}
		}else if([[spell valueForKey:@"saveEffect"] intValue]==0){
			return [NSNumber numberWithBool:NO];			
		}else{
			//no SR or attack roll and a save.
			return [NSNumber numberWithBool:YES];
		}			
		
	}
}
@end
